<?php

namespace App\Models\Cert;

use App\Models\Common\CommonModel;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class CertDriver extends CommonModel
{
    use HasFactory;

    protected $table = 'cert_driver';

    public function getUser() {
        return $this->hasOne(User::class,'id','user_id');
    }

    public function getRecords(array $conditions) {
        $records = $this->newQuery();
        if(!empty($conditions['phone'])){
            $records = $records->whereHas('getUser',function ($q) use ($conditions){
                $q->where('phone',$conditions['phone']);
            });
        }
        if(!empty($conditions['name'])){
            $records = $records->where('name','like','%'.$conditions['name'].'%');
        }

        if(!empty($conditions['getCertDate'])){
            $records = $records->whereDate('get_cert_day','>=',$conditions['getCertDate'][0])
                ->whereDate('get_cert_day','<=', $conditions['getCertDate'][1]);
        }
        if(!empty($conditions['ableDate'])){
            $records = $records->whereDate('effective_day','>=',$conditions['ableDate'][0])
                ->whereDate('effective_day','<=', $conditions['ableDate'][1]);
        }



        return $records->orderBy('id','desc');
    }


}
